package net.cloudcodex.client.home.presenter;

import java.util.ArrayList;
import java.util.List;

import net.cloudcodex.client.CloudCodexEventBus;
import net.cloudcodex.client.campaign.view.panel.CampaignPanel;
import net.cloudcodex.client.character.view.panel.CharacterPanel;
import net.cloudcodex.client.home.view.HomeCampaignView;
import net.cloudcodex.client.home.view.HomeCampaignViewImpl;
import net.cloudcodex.client.home.view.HomeView.Message;
import net.cloudcodex.client.home.view.panel.campaign.HomeCampaignPanel;
import net.cloudcodex.client.home.view.panel.character.HomeCharacterPanel;
import net.cloudcodex.client.widgets.CampaignAvatar;
import net.cloudcodex.shared.dto.NotificationDTO;
import net.cloudcodex.shared.dto.home.CampaignSummaryDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * 
 * 
 * @author ADaroussin
 */
@Presenter(view = HomeCampaignViewImpl.class,multiple=true)
public class HomeCampaignPresenter extends
		BasePresenter<HomeCampaignView, CloudCodexEventBus> {
	private final Message message;
	private CampaignSummaryDTO campaignSummaryDTO;

	@Inject
	public HomeCampaignPresenter(Message pMessage) {
		this.message = pMessage;
	}

	@Override
	public void bind() {
		this.view.getCampaignAvatar().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.selectCampaignTab(campaignSummaryDTO.getId(),CampaignPanel.CAMPAIGN_CONFIG);
				
			}
		});
		this.view.getSeeMoreAnchor().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.selectCampaignTab(campaignSummaryDTO.getId(),CampaignPanel.STORY_BOARD);
			}
		});
	}

	public void addCampaignSummaryDTO(CampaignSummaryDTO campaignSummaryDTO) {
		this.campaignSummaryDTO = campaignSummaryDTO;
	}


	public void initView() {
		refreshCampaignConfig();

		
		view.addCampaignInfos(campaignSummaryDTO.getCreationDate(),campaignSummaryDTO.getActiveCharacters());
		
		List<NotificationDTO> campaignNotificationDTOs = campaignSummaryDTO.getNotifications();
		if (campaignNotificationDTOs != null) {
			for (NotificationDTO notificationDTO : campaignNotificationDTOs) {
				view.addNotification(notificationDTO.getMessage(),notificationDTO.getDate());
			}
		}


		view.addStoryBoardResponse(new ArrayList<String>());
		
		view.initView();
		
	}

	/**
	 * 
	 */
	public void refreshCampaignConfig() {
		view.addCampaigns(campaignSummaryDTO.getId(),
				campaignSummaryDTO.getIcon(), campaignSummaryDTO.getName());
	}

}
